/*
 * Tic-Tac-Toe - Turn-based strategy game
 * Copyright 2008, 2009 Shayne Riley and Paul Maseberg
 *
 * Tic-Tac-Toe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tic-Tac-Toe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Tic-Tac-Toe.  If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner at gmail dot com>
 */

#include "Button.h"

Button::Button(int leftX, int lowerY, int width, int height) :
    mLeftX(leftX), mLowerY(lowerY), mWidth(width), mHeight(height),
    clicked(false), clicking(false)
{
	mLatestMouse = Graphics::Mouse();
	mLatestMouse.button0 = false;
	mLatestMouse.mouseOn = false;
	mLatestMouse.mouseX = 0;
	mLatestMouse.mouseY = 0;
}

Button::~Button()
{
}

void Button::update(const Graphics::Mouse& mouse)
{
    // The actual "update" occurs at the very end of the method.

    // The clicked state that occurred previously is now no longer valid.
    if (clicked)
    {
        clicked = false;
    }

    // If the user is clicking, and the mouse button is not pressed, it is
    // now a complete "click" because the user just released the button.
    if (isPressing() && !mouse.button0)
    {
        clicked = true;
        clicking = false;
    }
    // Otherwise, we should check to see if the user *just* started to click
    // the button. That is:
    // - Mouse is located over button.
    // - mouse button is pressed.
    // - mouse button wasn't pressed previously.
    else if (isMouseInRegion(mouse) && mouse.button0 && !mLatestMouse.button0)
    {
        clicking = true;
    }
    else if (!mouse.button0)
    {
        clicking = false;
    }

    // The actual update.
    mLatestMouse = mouse;
}

bool Button::isHovering()
{
    // True if:
    // - mouse is located over button.
    // - mouse button isn't being clicked.
    return !mLatestMouse.button0 && isMouseInRegion(mLatestMouse);
}

bool Button::isPressing()
{
    // True if:
    // - mouse button was pressed down while over the button.
    // - mouse is located on the button.
    return clicking && isMouseInRegion(mLatestMouse);
}

bool Button::isClicked()
{
    return clicked;
}

bool Button::isMouseInRegion(const Graphics::Mouse& mouse)
{
    // True if mouse is within bounds of the area, and the mouse is active
    // inside the app.
    return mLeftX <= mouse.mouseX && mouse.mouseX < mLeftX + mWidth &&
            mLowerY <= mouse.mouseY && mouse.mouseY < mLowerY + mHeight &&
            mouse.mouseOn;
}
